package com.chcplayer
{
import com.chcplayer.enums.CHEventName;

import flash.display.BitmapData;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.IEventDispatcher;
import flash.filesystem.File;
import flash.filesystem.FileMode;
import flash.filesystem.FileStream;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.images.JPEGDecoder;
import flash.images.PNGDecoder;
import flash.utils.ByteArray;

/**
 * author yh
 */
public class ChcFileModel extends EventDispatcher
{
	/**
	 */
	private var m_blNeedSave:Boolean;
	/**
	 */
	private var m_obPndDecoder:PNGDecoder = new PNGDecoder();
	/**
	 */
	private var m_rectBgEx:Rectangle = new Rectangle();
	/**
	 */
	private var m_nBgColor:int = 0xffFFff;
	/**
	 */
	private var m_nScaling:Number = 1;
	/**
	 * 临时背景色$模式
	 */
	private var m_blTempBgColoring:Boolean;
	private var m_nTempBgColor:int;
	/**
	 */
	private var m_btaBg:ByteArray;
	private var m_btaPic:ByteArray;
	private var m_strBgType:String;
	private var m_strPicType:String;
	/**
	 */
	private var m_nPicX:int;
	private var m_nPicY:int;
	/**
	 */
	private var m_obJD:JPEGDecoder;
	/**
	 */
	private var m_bmdPic:BitmapData;
	/**
	 */
	private var m_bmdBg:BitmapData;
	/**
	 */
	public function ChcFileModel(target:IEventDispatcher=null)
	{
		super(target);
	}
//--------------------------------------------------------------------------------------------------
// private
//--------------------------------------------------------------------------------------------------
	private function _readPic(bta:ByteArray, strEx:String, strDName:String):void
	{
		if ( this[strDName] != null )
		{
			BitmapData(this[strDName]).dispose();
			this[strDName] = null;
		}
		if ( "png" == strEx )
		{
			m_obPndDecoder.clear();
			m_obPndDecoder.decode(bta);
			this[strDName] = m_obPndDecoder.bitmapData;
		}
		else if ( "jpg" == strEx )
		{
			m_obJD = new JPEGDecoder(bta);
			/*
			*/
			var bmd:BitmapData = new BitmapData(m_obJD.width, m_obJD.height);
			this[strDName] = bmd;
			/*
			*/
			var ps:Vector.<uint> = m_obJD.pixels;
			var nL:uint = ps.length;
			var i:uint = 0;
			var nX:int;
			var nY:int;
			while ( i < nL )
			{
				nX = i % m_obJD.width;
				nY = i / m_obJD.width;
				bmd.setPixel32(nX, nY, ps[i]);
				i++;
			}
		}
	}
//==================================================================================================
// public
//==================================================================================================
	/**
	 */
	public function createNewFile():void
	{
		m_nScaling = 1;
		m_bmdBg = null;
		m_bmdPic = null;
		m_btaBg = null;
		m_btaPic = null;
		m_nPicX = 0;
		m_nPicY = 0;
		this.dispatchEvent(new Event(CHEventName.CHC_FILE_CHANGE) );
		this.setNeedSaveFile(true);
	}
	/**
	 */
	public function setPic(fi:File):void
	{
		m_btaPic = new ByteArray();
		var fs:FileStream = new FileStream();
		fs.open(fi, FileMode.READ);
		fs.readBytes(m_btaPic);
		fs.close();
		m_strPicType = fi.extension.toLowerCase();
		this._readPic(m_btaPic, m_strPicType, "m_bmdPic");
		/*
		*/
		this.setNeedSaveFile(true);
		this.dispatchEvent(new Event(CHEventName.PIC_CHANGE) );
	}
	/**
	 */
	public function setBgPic(fi:File):void
	{
		m_btaBg = new ByteArray();
		var fs:FileStream = new FileStream();
		fs.open(fi, FileMode.READ);
		fs.readBytes(m_btaBg);
		fs.close();
		m_strBgType = fi.extension.toLowerCase();
		this._readPic(m_btaBg, m_strBgType, "m_bmdBg");
		/*
		*/
		this.setNeedSaveFile(true);
		this.dispatchEvent(new Event(CHEventName.BG_CHANGE) );
	}
	/**
	 */
	public function setScaling(nScal:Number):void
	{
		m_nScaling = nScal;
		this.setNeedSaveFile(true);
		this.dispatchEvent(new Event(CHEventName.SCALING_CHANGE) );
	}
	/**
	 */
	public function getScaling():Number
	{
		return m_nScaling;
	}
	/**
	 */
	public function getPicBmd():BitmapData
	{
		return m_bmdPic;
	}
	/**
	 */
	public function getBgBmd():BitmapData
	{
		return m_bmdBg;
	}
	/**
	 */
	public function setBgRectEx(rect:Rectangle):void
	{
		if ( rect != null )
		{
			m_rectBgEx = rect.clone();
			this.setNeedSaveFile(true);
			this.dispatchEvent(new Event(CHEventName.BG_EX_CHANGE) );
		}
	}
	/**
	 */
	public function getBgRectEx():Rectangle
	{
		return m_rectBgEx.clone();
	}
	/**
	 * 临时背景色模式
	 */
	public function setTempBgColoring(bl:Boolean):void
	{
		m_blTempBgColoring = bl;
		this.dispatchEvent(new Event(CHEventName.BG_EX_CHANGE) );
	}
	/**
	 * 临时背景色值
	 */
	public function setTempBgColor(nC:int):void
	{
		m_nTempBgColor = nC;
		this.dispatchEvent(new Event(CHEventName.BG_EX_CHANGE) );
	}
	/**
	 */
	public function saveFile(fi:File):void
	{
		this.dispatchEvent(new Event(CHEventName.ON_FILE_SAVE) );
		var fs:FileStream = new FileStream();
		fs.open(fi, FileMode.WRITE);
		fs.writeByte(1);
		fs.writeByte(0);
		fs.writeFloat(m_nScaling);
		fs.writeInt(m_nBgColor);
		fs.writeInt(m_rectBgEx.x);
		fs.writeInt(m_rectBgEx.y);
		fs.writeInt(m_rectBgEx.width);
		fs.writeInt(m_rectBgEx.height);
		if ( m_btaBg != null )
		{
			fs.writeBoolean(true);
			fs.writeUTF(m_strBgType);
			fs.writeUnsignedInt(m_btaBg.length);
			fs.writeBytes(m_btaBg);
		}
		else
		{
			fs.writeBoolean(false);
		}
		if ( m_btaPic != null )
		{
			fs.writeBoolean(true);
			fs.writeUTF(m_strPicType);
			fs.writeUnsignedInt(m_btaPic.length);
			fs.writeBytes(m_btaPic);
			fs.writeInt(m_nPicX);
			fs.writeInt(m_nPicY);
		}
		else
		{
			fs.writeBoolean(false);
		}
		fs.writeBoolean(false);
		fs.close();
		this.setNeedSaveFile(false);
	}
	/**
	 */
	public function openFile(fi:File):void
	{
		var fs:FileStream = new FileStream();
		fs.open(fi, FileMode.READ);
		fs.readByte();
		fs.readByte();
		m_nScaling = fs.readFloat();
		m_nBgColor = fs.readInt();
		m_rectBgEx.x = fs.readInt();
		m_rectBgEx.y = fs.readInt();
		m_rectBgEx.width = fs.readInt();
		m_rectBgEx.height = fs.readInt();
		if ( fs.readBoolean() )
		{
			m_btaBg = new ByteArray();
			m_strBgType = fs.readUTF();
			fs.readBytes(m_btaBg, 0, fs.readUnsignedInt());
			this._readPic(m_btaBg, m_strBgType, "m_bmdBg");
		}
		else
		{
			m_btaBg = null;
		}
		if ( fs.readBoolean() )
		{
			m_btaPic = new ByteArray();
			m_strPicType = fs.readUTF();
			fs.readBytes(m_btaPic, 0, fs.readUnsignedInt());
			this._readPic(m_btaPic, m_strPicType, "m_bmdPic");
			m_nPicX = fs.readInt();
			m_nPicY = fs.readInt();
		}
		else
		{
			m_btaPic = null;
		}
		fs.close();
		this.dispatchEvent(new Event(CHEventName.CHC_FILE_CHANGE) );
		this.setNeedSaveFile(false);
	}
	/**
	 * 设置背景色
	 */
	public function setBgColor(nC:int):void
	{
		if ( m_nBgColor != nC )
		{
			m_nBgColor = nC;
			this.setNeedSaveFile(true);
			this.dispatchEvent(new Event(CHEventName.BG_EX_CHANGE) );
		}
	}
	/**
	 */
	public function setPicLocation(nX:int, nY:int):void
	{
		if ( m_nPicX != nX || m_nPicY != nY )
		{
			m_nPicX = nX;
			m_nPicY = nY;
			this.setNeedSaveFile(true);
		}
	}
	/**
	 */
	public function getPicLocation():Point
	{
		return new Point(m_nPicX, m_nPicY);
	}
	/**
	 */
	public function getBgColor():int
	{
		if ( m_blTempBgColoring )
		{
			return m_nTempBgColor;
		}
		return m_nBgColor;
	}
	/**
	 */
	private function setNeedSaveFile(bl:Boolean):void
	{
		m_blNeedSave = bl;
		this.dispatchEvent(new Event(Event.CHANGE) );
	}
	/**
	 */
	public function getNeedSaveFile():Boolean
	{
		return m_blNeedSave;
	}
}
}